<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * Subclass for representing a row from the 'users' table.
 *
 * 
 *
 * @package lib.model
 */ 
class User extends BaseUser
{
	/**
	 * Returns the location to the user's avatar image
	 * @see lib/model/om/BaseUser#getAvatar()
	 * @param $size, the size desired. Possible options are null, and thumb
	 * @return String, path to the user's avatar image
	 */
	public function getAvatar($size = null) {
		if($size == 'thumb') {
			$thumbnail = dirname($this->avatar)."/thumb-".basename($this->avatar);
			return $thumbnail;
		} else {
			return $this->avatar;
		}
	}
	
	public function getProf(){
		return ProfPeer::retrieveByUserId($this->getId());
	}
	
	public function getFacebookAvatar() {
		// The 'avatar' field on the users table stores the complete url for a facebook user's profile picture.
		return $this->avatar;
	}
	
	/**
	 * Returns the user's first name and last name concatenated together
	 * @return String
	 */
	public function getFullName() {
		return $this->getFirstName() . ' ' . $this->getLastName();
	}
	

	/*Added by CLH. getHlFullName() (get hyper-linked full name) returns the full name hyperlinked to user's profile/show page*/
	
	public function getHlFullName() {
		return link_to($this->getFullName(),'profiles/show?id='.$this->getId());
	}
	

	public function getNetworkDisplayName() {
		$networks = UserPeer::$networks;
		return $networks[$this->getNetworkId()];
	}
	
	public function isMember($group_id){
		$q = new Criteria();
		$q->add($q->getNewCriterion(GroupMemberPeer::USER_ID,$this->getId(),Criteria::EQUAL));
		$q->add($q->getNewCriterion(GroupMemberPeer::GROUP_ID,$group_id,Criteria::EQUAL));
		$test = GroupMemberPeer::doSelect($q);
		if (!empty($test)){
			return 1; //is a member
		}
		else{
			return 0; //is not a member
		}
	}
	
	public function isAdminOf($course_id){
		$c = new Criteria();
		$c->add(CourseAdminPeer::COURSE_ID, $course_id);
		$c->add(CourseAdminPeer::USER_ID, $this->getId());
		$ca = CourseAdminPeer::doSelectOne($c);
		if($ca)
			return true;
		return false;	
	}
	
	function isFacebookUser() {
		if ($this->getFacebookId() == null) {
			return false;
		}
		return true;
	}
	
	public function isLoggedInFacebookUser() {
		if (!$this->isFacebookUser())
			return false;
		
		try {
			FacebookUtility::get_info(709259, 'first_name');
		} catch (FacebookRestClientException $ex) {
			return false;
		}
		return true;
	}

	
	public function __toString() {
	
		return $this->getFullName();
	}
	/* CLH - Simply returns a hyperlinked name for the object */
	public function renderAsRecipient(){
		return "<a href = '/profiles/show/id/".$this->getId()."'>".$this->getFullName()."</a>";
	} 

	public function save(PropelPDO $con = null) {
		parent::save($con);		// we need to save the user first
		$this->updateLuceneIndex();  // update Lucene index
	}

	public function delete(PropelPDO $con = null)
	{
		$index = Search::getLuceneIndex(Search::USER);
		$term  = new Zend_Search_Lucene_Index_Term($this->getId(), 'pk');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$hit   = $index->find($query);

		if ($hit) {
  			foreach($hit as $h) { $index->delete($h->id); }
		}

		parent::delete($con);
	}

	public function updateLuceneIndex() {
		$index = Search::getLuceneIndex(Search::USER);
 
  		// remove an existing entry
		$term  = new Zend_Search_Lucene_Index_Term($this->getId(), 'pk');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$hit   = $index->find($query);

  		if ($hit) {
    			foreach($hit as $h) { $index->delete($h->id); }
  		}
 
  		$doc = new Zend_Search_Lucene_Document();
 
	  	// store primary key URL to identify it in the search results
	  	$doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $this->getId()));
 
	  	// index fields
	  	$doc->addField(Zend_Search_Lucene_Field::Text('first_name', $this->getFirstName()));
		$doc->addField(Zend_Search_Lucene_Field::Text('last_name', $this->getLastName()));
		$doc->addField(Zend_Search_Lucene_Field::Text('username', $this->getUsername()));
		$doc->addField(Zend_Search_Lucene_Field::UnIndexed('created',$this->getCreatedAt()));

		$user_profile = UserProfilePeer::retrieveByUserId($this->getId());
		if($user_profile) {
			$doc->addField(Zend_Search_Lucene_Field::Text('books', $user_profile->getFavoriteBooks()));
			$doc->addField(Zend_Search_Lucene_Field::Text('city', $user_profile->getCity()));
			$doc->addField(Zend_Search_Lucene_Field::Text('profile', $user_profile->getAboutMe()));
		}

  		// add to the index
  		$index->addDocument($doc);
  		$index->commit();
  	}
}
